package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.modular.netcar.dao.AppVersionMapper;
import com.stylefeng.guns.modular.netcar.model.AppVersion;
import com.stylefeng.guns.modular.netcar.service.IAppVersionService;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**

 * APP版本设置 Service 实现 
 * @author sasazaixian
 * @Date 2018-10-23 11:02
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper,AppVersion> implements IAppVersionService {

    @Resource
    private AppVersionMapper appVersionMapper;

    /**
     * 删除APP版本设置
     * @param appVersion
     * @return     
     */
    @Transactional
    public int deleteAppVersion(AppVersion appVersion,String token)throws Exception{
        if(appVersion==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.appVersionMapper.deleteAppVersion(appVersion);
    }
    /**
     * 批量删除APP版本设置
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchAppVersion(String[] ids,String token)throws Exception{
        return this.appVersionMapper.deleteBatchAppVersion(ids);
    }
    /**
     * 查询APP版本设置列表
     * @param appVersion APP版本设置对象
     * @return
     */
    public List<AppVersion> selectListAppVersion(AppVersion appVersion,String token)throws Exception{
        return  this.appVersionMapper.selectListAppVersion(appVersion);
    }
    /**
	 * 分页查询APP版本设置列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListAppVersion(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(appVersionMapper.selectListAppVersion(page,(AppVersion)pager.getObj()));
        List<AppVersion> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param appVersion APP版本设置对象
     * @return 
     */
    public  List<AppVersion> selectListAppVersionForUpdate(AppVersion appVersion)throws Exception{
        return  this.appVersionMapper.selectListAppVersionForUpdate(appVersion);
    }
    /**
     * 通过主键id查询的APP版本设置列表
     * @param id 主键id
     * @return     
     */
    public  AppVersion
    findAppVersionById(String id,String token)throws Exception{
        List<AppVersion> listAppVersion=selectListAppVersion(null,token);
        if(listAppVersion!=null && listAppVersion.size()>0 ){
            return listAppVersion.get(0);
        }else{
            return null;
        }
    }

    /**
     * 新增APP版本设置数据
     * @param appVersion  APP版本设置对象
     * @return
     */
    @Transactional
    public int insertAppVersion(AppVersion appVersion,String token) throws Exception {

        appVersion.setUpdateTime(new Date());
        appVersion.setUpdateUser(JwtTokenUtil.getClaimFromToken(token).getSubject());

        List<AppVersion> selAppVersion= selectListAppVersionForUpdate(null);
        if(selAppVersion==null || selAppVersion.size()==0){
            if(appVersion!=null && (appVersion.getAvId()==null || appVersion.getAvId().trim().length()<10)){
                appVersion.setAvId(IdWorker.get32UUID());
            }
            this.appVersionMapper.insertAppVersion(appVersion);
        }else{
            appVersion.setAvId(selAppVersion.get(0).getAvId());
            this.appVersionMapper.updateAppVersion(appVersion);
        }
        return 0;//返回主键
    }
    
    /**
     * 更新APP版本设置数据
     * @param appVersion APP版本设置对象
     * @return     
     */
    @Transactional
    public int updateAppVersion(AppVersion appVersion,String token) throws Exception {     	
        return  this.appVersionMapper.updateAppVersion(appVersion);
    }    
    /**
     * 查询APP版本设置返回key-Value属性
     * @param appVersion 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsAppVersion(AppVersion appVersion,String token) throws Exception {
        List<AppVersion> listAppVersion=this.appVersionMapper.selectListAppVersion(appVersion);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(AppVersion item:listAppVersion){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getAvId());
            opts.setValue(item.getAvUpdateContent());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
